package slices
{
	import flash.display.DisplayObject;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	
	
	public class TransformColor extends TimeSlice
	{
		public var toColor:ColorTransform;
		private var _start:int;
		public var speed:Number;
		public var target:DisplayObject;
		public var fromColor:ColorTransform;
		public var cost:int;
		
		public function TransformColor(target:DisplayObject, ct:ColorTransform, speed:Number)
		{
			super();
			this.target = target;
			this.toColor = ct;
			this.speed = speed;
		}
		
		override public function onStart():void {
			_start = _now;
			cost = Math.abs(target.transform.colorTransform.redMultiplier-toColor.redMultiplier)*1000/speed;
			fromColor = target.transform.colorTransform;
		}
		
		override public function onUpdate():void {
			var t:int = _now - _start;
			if (t >= cost) {
				target.transform.colorTransform = toColor;
				isDone = true;
			} else {
				var ct:ColorTransform = new ColorTransform();
				var p:Number = t/cost;
				ct.redMultiplier = fromColor.redMultiplier - (fromColor.redMultiplier-toColor.redMultiplier) * p;
				ct.greenMultiplier = fromColor.greenMultiplier - (fromColor.greenMultiplier-toColor.greenMultiplier) * p;
				ct.blueMultiplier = fromColor.blueMultiplier - (fromColor.blueMultiplier-toColor.blueMultiplier) * p;
				target.transform.colorTransform = ct;
			}
		}
	}
}